
#ifndef _COMMUNICATOR_H_
#define	_COMMUNICATOR_H_	1


#include <Winsock.h>

#include "httpcommon.h"
#include "RequestMessageStream.h"
#include "ResponseMessageStream.h"


typedef enum	_COMM_STATE	{ CS_READY_TO_RECV_REQ=1,
								CS_READY_TO_SEND_REQ=2,
								CS_READY_TO_RECV_RES=4,
								CS_READY_TO_SEND_RES=8,
								CS_COMPLETED=16 } COMM_STATE;

const int	GRACEFUL_CLOSE = 0x0;



class CHttpCommunicator
{
public:
	CHttpCommunicator() :
	  m_ReqStream(0),m_ResStream(0),
	  m_ReqStreamLen(0), m_ResStreamLen(0),
	  m_IsReqMade(FALSE), m_IsResMade(FALSE),
	  m_CommState(CS_READY_TO_RECV_REQ)
	{
		memset(&m_hpd, 0, sizeof(m_hpd));
	}

	~CHttpCommunicator();

	// GET/SET Functions
	CRequestMessageStream &		GetHttpRequest() { return m_Request; }
	CResponseMessageStream &	GetHttpResponse() { return m_Response; }

	HTTP_PROXY_DATA		GetHttpProxyData() { return m_hpd; }
	void				SetHttpProxyData(HTTP_PROXY_DATA *hpd);

	BOOL	readProxyHttpRequest();
	BOOL	sendProxyHttpRequest();
	BOOL	readProxyHttpResponse();
	BOOL	sendProxyHttpResponse();
	
	void	CleanupHttpComm();

	COMM_STATE	GetCommunicateState() { return m_CommState; }

private:
	BOOL	readHttpRequest(SOCKET &sock);
	BOOL	sendHttpRequest(SOCKET &sock);
	BOOL	readHttpResponse(SOCKET &sock);
	BOOL	sendHttpResponse(SOCKET &sock);

	BOOL	MakeRequestMessage(char *frag, unsigned fragLen);
	BOOL	MakeResponseMessage(char *frag, unsigned fragLen);

	BOOL	MakeWebServerInfo();


private:
	CRequestMessageStream	m_Request;
	CResponseMessageStream	m_Response;

	char *		m_ReqStream;
	char *		m_ResStream;
	unsigned	m_ReqStreamLen;
	unsigned	m_ResStreamLen;

	// flags
	BOOL		m_IsReqMade;
	BOOL		m_IsResMade;
	
	COMM_STATE	m_CommState;


	HTTP_PROXY_DATA		m_hpd;
};




#endif